<?php
/*
 *
 * creaza si gestioneaza un capitol
 *
 */
require_once 'pdfPrinter.class.php';

class xmlChapter {
	//Error messages
	const CREATEERROR = "The file could not be created!";
	const CLOSEERROR = "The file could not be closed!";
	const WRITEEROR = "The xml structure could not be created!";
	const ERRNEWSCHAP = "Error creating new chapter";

	/**
	 * numele capitolului
	 * @access private
	 */
	private $chapterName;

	public $chapterId;

	/**
	 * calea documentului
	 * @access private
	 */
	public $chapterPath;

	/**
	 * handlerul documentului
	 * @access private
	 */
	public $chapterDoc;

	/**
	 * nodul radacina al documentului
	 * @access public
	 */
	public $chapterRoot;

	/**
	 * constructor
	 * creaza un nou capitol(creaza un nou fisier pt acesta)
	 * @param string $chapName numele capitolului
	 * @param string $chapId id-ul capitolului
	 * @param string $path calea catre noul capitol
	 * @return DOMElement or Exception if errors occur
	 */
	public function __construct($chapName, $chapId, $docPathTo) {
		//verificare parametri

		//denumire fisier
		$newChapterName = $docPathTo ."/". $chapName . ".xml";
		//echo $newChapterName;
		//verificare daca doc deja exista
		if (!file_exists($newChapterName)) {
			$newChapterFile = fopen($newChapterName, "w");
			if ($newChapterFile) {
				$closedOk = fclose($newChapterFile);
				if (!$closedOk) {
					throw new Exception(self :: CLOSEERROR);
				}
			} else {
				throw new Exception(self :: CREATEERROR);
			}

			//creare element DOMElement
			$basicStruct = "<chapter chapId='" . $chapId . "'>" .
			"<title>" . $chapName . "</title></chapter>";
			$this->chapterDoc = new DOMDocument('1.0');
			$this->chapterDoc->loadXML($basicStruct);
			$this->chapterRoot = & $this->chapterDoc->documentElement;
			$this->chapterDoc->save($newChapterName);
			$this->chapterPath = $newChapterName;
			$this->chapterId = $chapId;
		}else{
			//echo "oki doki";
			$this->chapterDoc=& new DOMDocument('1.0');
			$this->chapterDoc->load($newChapterName);
			$this->chapterRoot=& $this->chapterDoc->documentElement;
			$this->chapterPath=$newChapterName;
			$this->chapterId=$chapId;
		}
	}

	public function createNewParagraph($id, $text) {
		$newParagr = new xmlBasicElements(& $this->chapterRoot, & $this->chapterDoc);
		$newParagr->createParagraph($id, $text);
		$this->chapterDoc->save($this->chapterPath);
	}

	public function updateParagraph($id, $text) {
		$newParagr = new xmlBasicElements($this->chapterRoot, $this->chapterDoc);
		$newParagr->updateParagraph($id, $text);
		$this->chapterDoc->save($this->chapterPath);
	}

	/**
	 * creare sectiune imagine
	 * @access public
	 * @param string $id atributul ID al imaginii
	 * @param string $src path-ul imaginii
	 * @return bool TRUE if no error occurs
	 */
	public function createNewImage($id, $src) {
		$newImg = new xmlBasicElements(& $this->chapterRoot, & $this->chapterDoc);
		try {
			$newImg->createImage($id, $src);
			$this->chapterDoc->save($this->chapterPath);
			return true;
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	/**
	 * creare sectiune table
	 * throws exceptions
	 * @access public
	 * @param string $idTable id-ul campului <table>
	 * @param array[array] $tRows liniile tabelului
	 * @param array[array] $tHeader headerul tabelului
	 * @param string [$tCaption] denumirea tabelului
	 * @return bool TRUE if no error occurs
	 */
	public function createNewTable($idTable, $tRows, $tCaption = '', $tHeader = '') {
		$newTbl = new xmlBasicElements(& $this->chapterRoot, & $this->chapterDoc);
		//test nr elemente coloane!!!
		try {
			$newTbl->createTable($idTable, $tRows, $tCaption, $tHeader);
			$this->chapterDoc->save($this->chapterPath);
			return true;
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	/**
	 * creare sectiune list
	 * throws exceptions
	 * @access public
	 * @param string $idList id-ul tagului <list>
	 * @param string $listType tipul listei default 'unordered'
	 * @param array $listElem elementele listei
	 * @return bool TRUE if no error occurs
	 */
	public function createNewList($idList, $listElem, $listType = 'unordered') {
		$newList = new xmlBasicElements(& $this->chapterRoot, & $this->chapterDoc);
		try {
			$newList->createList($idList, $listElem, $listType);
			$this->chapterDoc->save($this->chapterPath);
			return true;
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	/**
	 * creare sectiune note
	 * throws exceptions
	 * @access public
	 * @param $string $noteId id-ul tagului note
	 * @param $string $noteContent
	 * @return bool TRUE if no error occurs
	 */
	public function createNewNote($noteId, $noteContent) {
		$newNote = new xmlBasicElements(& $this->chapterRoot, & $this->chapterDoc);
		try {
			$newNote->createNote($noteId, $noteContent);
			$this->chapterDoc->save($this->chapterPath);
			return true;
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	/**
	 * creare sectiune attention
	 * throws exceptions
	 * @access public
	 * @param $string $attId id-ul tagului attention
	 * @param $string $attContent
	 * @return bool TRUE if no error occurs
	 */
	public function createNewAttention($attId, $attContent) {
		$newAtt = new xmlBasicElements(& $this->chapterRoot, & $this->chapterDoc);
		try {
			$newAtt->createAttention($attId, $attContent);
			$this->chapterDoc->save($this->chapterPath);
			return true;
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	/**
	 * creare sectiune quote
	 * throws exceptions
	 * @access public
	 * @param $string $quoteId id-ul tagului quote
	 * @param $string $quoteContent
	 * @return bool TRUE if no error occurs
	 */
	public function createNewQuote($quoteId, $quoteContent) {
		$newNote = new xmlBasicElements(& $this->chapterRoot, & $this->chapterDoc);
		try {
			$newNote->createQuote($quoteId, $quoteContent);
			$this->chapterDoc->save($this->chapterPath);
			return true;
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	public function createNewSubChapter($id, $title) {
		$newSubChap = new xmlSubChapter(& $this->chapterRoot, & $this->chapterDoc, $this->chapterPath, $id, $title);
		try {
			$this->chapterDoc->save($this->chapterPath);
			$this->chapterElements[$id] = & $newSubChap;
			return $this->chapterElements[$id];
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	public function createNewSection($id, $title) {
		$newSection = new xmlSection(& $this->chapterRoot, & $this->chapterDoc, $this->chapterPath, $id, $title);
		try {
			$this->chapterDoc->save($this->chapterPath);
			$this->chapterElements[$id] = & $newSection;
			return $this->chapterElements[$id];
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	public function getRoot() {
		return $this->chapterRoot;
	}

	public function printChapterToPdf(){
		$pdfPrinter=new pdfPrinter();

		foreach ($this->chapterRoot->childNodes as $node){
			$nodeType=$node->nodeName;
			switch ($nodeType){
				case "title":
					$pdfPrinter->printTitle($node->nodeValue);
				break;
				
				case "paragraph":
					$pdfPrinter->printParagraph($node->nodeValue);
				break;
				
				case "note":
					$pdfPrinter->printNote($node->nodeValue);
				break;

				case "quote":
					$pdfPrinter->printQuote($node->nodeValue);
				break;
				
				case "attention":
					$pdfPrinter->printAttention($node->nodeValue);
				break;
				
				case "image":
					$path=$node->getElementsByTagName('imgSrc')->item(0)->nodeValue.".".$node->getElementsByTagName('imgMIME')->item(0)->nodeValue;
					$caption=$node->getElementsByTagName('imgCaption')->item(0)->nodeValue;
					$pdfPrinter->printImage($path, $caption);
				break;
				
				case "table":
					$info=$node->getElementsByTagName('tableCaption')->item(0)->nodeValue;
					
					$header=array();
					$THeader=$node->getElementsByTagName('tableHeader')->item(0)->getElementsByTagName('tableRow')->item(0)->getElementsByTagName('tableColumn');
					//var_dump($THeader);
					foreach ($THeader as $th){
						$header[]=$th->nodeValue;
					}
					//var_dump($header);
					$colNr=count($header);
					
					$data=array();
					$TRowsList=$node->getElementsByTagName('tableRow');
					//var_dump($TData);
					foreach($TRowsList as $trl){
						//var_dump($trl);
						if((int)$trl->getAttribute(rowIndex) !== 0){
							//var_dump($trl->getAttribute(rowIndex));
							$TColsList=$trl->getElementsByTagName('tableColumn');
							$newLine=array();
							foreach ($TColsList as $tcl){
								//var_dump($tcl);
								$newLine[]=$tcl->nodeValue;
							}
							$data[]=$newLine;
						}
					}
					//var_dump($data);
					
					$pdfPrinter->printTable($header, $data, $info);
				break;
			}
				
		}

		$pdfPrinter->Output();
		//var_dump($this->chapterRoot);
	}
}
?>